// pages/collectiontravel/collectiontravel.js

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex:1,
    rows:10,
    collecAry:[
      
    ],
  },
  clickCollec_item:function(data){
    console.log(data.detail,'子组件传值');
    

    this.fromCollectAddTravel(data.detail);

    
  },
  /**
   * 从收藏中增加用户行程 fromCollectAddTravel
   */
  fromCollectAddTravel:function(id){
    app.apiPost(app.apiList.fromCollectAddTravel, {
      content: {
        travelId: id
      }
    }, (res) => {
      console.log(res,'从收藏中增加用户行程');

      var obj = {
        id: res.content.id,
        from_name: 'collection',
        page_type: 2
      }
      /* 1:行程计划  2：自助行程-从收藏的行程中创建*/
      obj = JSON.stringify(obj);
      wx.navigateTo({
        url: '../collecrecomend/collecrecomend?obj=' + obj
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.gainLoadingListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉刷新');
    let that = this;
    that.setData({
      pageIndex:1,//每次触发下拉时间pageIndex=0
    })
    that.gainLoadingListData();
  },
  /**
   *初始化数据或是下拉刷新数据
  */
  gainLoadingListData:function(){
    let that = this;
    let pageIndex = that.data.pageIndex;
    console.log('pageIndex==', pageIndex);

    app.apiPost(app.apiList.collectTravelList, {
      content:{
        pageNum: this.data.pageIndex,
        pageSize: this.data.rows
      } 
    }, (res) => {
      wx.stopPullDownRefresh(); // 数据请求成功后，停止刷新
      console.log('初始化数据或是下拉刷新数据',res);
      var list = res.content.list;
      that.setData({
        collecAry: list
      })
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("上拉加载");
    let that = this;
    that.setData({
      loading: true,  //把"上拉加载"的变量设为false，显示 
      pageIndex: that.data.pageIndex + 1
    })
    // 上拉获取更多数据
    this.gainMoreLoadingListData()
  },
  /*
  *上拉获取更多数据
  */
  gainMoreLoadingListData:function(){
    var that = this;
    app.apiPost(app.apiList.collectTravelList, {
      content:{
        pageNum: this.data.pageIndex,
        pageSize: this.data.rows
      } 
    }, (res) => {
      var list = res.content.list;

      var ary = that.data.collecAry;
      ary = ary.concat(list);

      that.setData({
        collecAry: ary
      })
      if(res.length>0){

        that.setData({
          // collecAry: array,
          loading: true,  //把"上拉加载"的变量设为false，显示 
        })

      } else {// 数组为空

        that.setData({
          loading: false,  //把"上拉加载"的变量设为true，隐藏
          loaded: true,  //把"上拉加载完成"的变量设为false，显示

        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    let pageIndex = that.data.pageIndex;
    console.log("pageIndex == ", pageIndex);

  }
})

// https://www.jianshu.com/p/da4e6dfe4ffd